<template>
  <div>
    <el-dialog
      :title="bannerForm.id ? '编辑' : '新增'"
      :visible.sync="is.show"
      :before-close="cancel"
    >
      <el-form :model="bannerForm" ref="bannerForm" :rules="rules">
        <!-- 轮播图名称 -->
        <el-form-item
          prop="title"
          label="轮播图名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="bannerForm.title" autocomplete="off"></el-input>
        </el-form-item>

        <!-- 图片 -->
        <el-form-item
          prop="img"
          label="轮播图名称"
          :label-width="formLabelWidth"
        >
          <el-upload
            action=""
            :on-change="changeImg"
            list-type="picture-card"
            :auto-upload="false"
            :limit="1"
            :on-exceed="
              () => {
                $message.error('超出上传限制');
              }
            "
            :file-list="arr"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>

        <!-- 状态 -->
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="bannerForm.status"
            active-color="#13ce66"
            inactive-color="#ccc"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="enter">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
//引入辅助性函数
import { mapState, mapActions } from "vuex";
//引入 添加 或 编辑接口
import { addOreditBanner, bannerInfo } from "../../request/api";
export default {
  data() {
    const uploadImg = (a, b, callback) => {
      if (this.bannerForm.img == "") {
        callback(new Error("请上传图片"));
      } else {
        callback();
      }
    };
    return {
      bannerForm: {
        title: "",
        img: "",
        status: 1,
      },
      formLabelWidth: "120px",
      arr: [], //用来数据回显
      //自定义验证规则
      rules: {
        title: { required: true, message: "请输入轮播图名称", trigger: "blur" },
        img: { validator: uploadImg, trigger: "blur" },
      },
    };
  },

  //计算属性 使用辅助性函数mapState
  computed: {
    ...mapState("banner", ["bannerList", "is"]),
  },

  //方法 使用辅助性函数 mapActions
  methods: {
    ...mapActions("banner", ["getBanner"]),

    //选择图片时触发
    changeImg(file) {
      this.bannerForm.img = file.raw;
    },

    //点击取消事件
    cancel() {
      //关闭弹窗
      this.$store.commit("banner/changeDialog", false);
      //清空列表
      this.bannerForm = {
        title: "",
        img: "",
        status: 1,
      };
      //清空表单验证规则
      this.$refs.bannerForm.resetFields();
      this.arr = [];
    },

    //点击确认事件
    enter() {
      let url = this.bannerForm.id ? "banneredit" : "banneradd";
      this.$refs.bannerForm.validate((val) => {
        if (val) {
          addOreditBanner(url, this.bannerForm).then((res) => {
            if (res.code == 200) {
              //成功提示
              this.$message.success(res.msg);
              //关闭弹窗
              this.cancel();
              //重新获取数据
              this.getBanner();
            } else {
              //失败提示
              this.$message.error(res.msg);
            }
          });
        }
      });
    },

    //数据回显的方法
    dataForm(id) {
      bannerInfo({ id }).then((res) => {
        //设置图片回显
        this.arr = [{ url: res.list.img }];
        this.bannerForm = res.list;
        //设置id
        this.bannerForm.id = id;
      });
    },
  },
};
</script>


<style scoped>
</style>